package org.lewis.man.controllers;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.lewis.common.cache.CacheInterceptor;
import org.lewis.common.domain.Song;
import org.lewis.common.page.Page;
import org.lewis.common.service.SongService;
import org.lewis.woman.service.SongServiceImpl;

import net.sf.cglib.proxy.Enhancer;

import com.tocersoft.framework.core.annotations.Controller;
import com.tocersoft.framework.web.annotations.Mapping;
import com.tocersoft.framework.web.annotations.NameSpace;
import com.tocersoft.framework.web.renderer.Renderer;
import com.tocersoft.framework.web.renderer.TemplateRenderer;

/**
 * 标签检索歌曲
 * 
 * @author taiqichao
 * 
 */
@Controller
@NameSpace("/song/tag")
public class SongTagController {

	private final static String VP = "/WEB-INF/views/music/";

	private final static int  count = 20;
	

	/**
	 * 标签检索歌曲
	 * 
	 * @return
	 * @throws Exception
	 */
	@Mapping("/$1")
	public Renderer seachSongByTag(String tagName) throws Exception {
		//tagName = URLDecoder.decode(tagName, "UTF-8");
		SongService songService = (SongService) Enhancer.create(SongServiceImpl.class, new CacheInterceptor());
		Page<Song> viewPage = songService.querySongByTag(tagName,new Page<Song>(1, count));
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("viewPage", viewPage);
		data.put("tagName", tagName);
		return new TemplateRenderer(VP + "song_tag_list.vm", data);
	}
	
	/**
	 * 标签检索歌曲分页查询
	 * @param tagName
	 * @return
	 * @throws Exception
	 */
	@Mapping("/$1/$2")
	public Renderer seachSongByTagPage(String tagName,int page) throws Exception {
		//tagName = URLDecoder.decode(tagName, "UTF-8");
		if(page<=0){
			page=1;
		}
		SongService songService = (SongService) Enhancer.create(SongServiceImpl.class, new CacheInterceptor());
		Page<Song> viewPage = songService.querySongByTag(tagName,new Page<Song>(page, count));
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("viewPage", viewPage);
		return new TemplateRenderer(VP + "song_item_list.vm", data);
	}
	

}
